//
//  YSGNurseSubscribeController.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/8/4.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit

class YSGNurseSubscribeController: BaseViewController {

    @IBOutlet weak var searchBar: UISearchBar!{
        didSet{
            searchBar.placeholder = "搜索护士姓名"
            searchBar.delegate = self
            searchBar.searchBarStyle = .minimal
        }
    }
    @IBOutlet weak var tableView: UITableView!{
        didSet{
            tableView.delegate = self
            tableView.dataSource = self
            
            tableView.register(DoctorReserveCell.nibForCell(),
                               forCellReuseIdentifier: DoctorReserveCell.reuseIdentifier())
            
            tableView.mj_header = refreshHeader(action: #selector(firstLoadData))

            tableView.mj_footer = refreshFooter(action: #selector(nextLoadData))
            
            tableView.mj_header?.beginRefreshing()
            
        }
    }
    private var viewModel: YSGNurseSubscribeViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    var current : Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "护理预约咨询"
    }
    
    
    @objc private func firstLoadData(){
        current = 1
        loadData()
    }
    
    @objc private func nextLoadData(){
        current = current + 1
        loadData()
    }
     private func loadData() {
        
        YSGNurseSubscribeViewModel.getNurseSubscribeList(name: searchBar.text ?? "", size: 10, current: current) {[weak self] models,total,errorMsg, code in
            self?.tableView.mj_header?.endRefreshing()
            self?.tableView.mj_footer?.endRefreshing()
            
            if (self!.current * 10) >= total {
                self?.tableView.mj_footer?.endRefreshingWithNoMoreData()
            }
            switch code {
            case .failure:
                HUDTool.showError(message: errorMsg)
            case .success:
                if self?.current == 1 {
                    self?.viewModel = YSGNurseSubscribeViewModel(models: models)
                } else {
                    self?.viewModel?.models.append(contentsOf: models)
                }
                self?.tableView.reloadData()
            }
        }
    }
}
extension YSGNurseSubscribeController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel?.numberOfSections ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: DoctorReserveCell.reuseIdentifier(),
                                                 for: indexPath) as! DoctorReserveCell
        
       
        let model = viewModel?.models[indexPath.section]
        cell.configureNurse(model: model!)
        
        return cell
    }
}

extension YSGNurseSubscribeController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 16))
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return viewModel?.cellHeight(index: indexPath.section) ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
        let model = viewModel?.models[indexPath.section]
        
        
        var doctorModel = DoctorDetail()
        doctorModel.tbUserId = model?.tbUserId
        doctorModel.docName = model?.name
        doctorModel.jobTitleName = model?.jobTitle
        doctorModel.deptName = model?.deptName
        doctorModel.score = model?.score?.floatValue ?? 0
        doctorModel.num = model?.consultCount ?? 0
        doctorModel.id = model?.doctorId
        doctorModel.thumbnailUrl = model?.doctorPicture
        doctorModel.docGoodAtNames = model?.introduction
        doctorModel.hospitalName = "咨询数 " + String(format: "%d", model?.consultCount ?? 0)
        let doctorDetail = DoctorDetailViewModel(model: doctorModel,
                                                 selectType: .nurse)
        let newAdvisoryVc = NewPMCAdvisoryController(with: doctorDetail)
        navigationController?.pushViewController(newAdvisoryVc, animated: true)
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        view.endEditing(true)
    }
}


extension YSGNurseSubscribeController :UISearchBarDelegate{
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        tableView.mj_header?.beginRefreshing()
        searchBar.endEditing(true)
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            firstLoadData()
        }
    }
}
